#include "routeadddialog.h"
#include "qrcutil.h"
#include "ui_routeadddialog.h"
#include "utils/route.h"
#include "utils/scripts.h"

#include <QCompleter>
#include <QMenu>
#include <QStringListModel>



RouteAddDialog::RouteAddDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::RouteAddDialog)
{
    ui->setupUi(this);

    auto interfaces = Scripts::bash_scripts(QR("://commands/interfaces")).split(QRegExp("\\s"), Qt::SkipEmptyParts);
    auto address = Scripts::bash_scripts(QR("://commands/address")).split(QRegExp("\\s"), Qt::SkipEmptyParts);

    foreach (auto interface, interfaces) {
        if (interface != "lo" || !interface.startsWith("br-")) {
            ui->static_combox_interface->addItem(interface);
        }
    }

    QStringList valid_address;
    foreach (auto addr, address) {
        if (!addr.startsWith("127.0.0.1") && !addr.endsWith(".0.1")) {
            valid_address << addr;
        }
    }

    QCompleter *completer = new QCompleter(this);
    QStringListModel *model = new QStringListModel(valid_address);
    completer->setModel(model);
    ui->static_ip->setCompleter(completer);
    {
        auto via_address = valid_address;
        for (int via_index = 0; via_index < via_address.count(); ++via_index) {
            auto via_addr = via_address[via_index];
            via_address[via_index] = via_addr.split(".").mid(0, 3).join(".") + ".1";
        }
        ui->static_via_ip->setCompleter(make_completer(via_address));
    }
    ui->static_lineedit_src->setCompleter(completer);
}

RouteAddDialog::~RouteAddDialog()
{
    delete ui;
}

void RouteAddDialog::setRoute(QString route)
{
    QString ip,via,dev,proto,scope,src,metric;
    Route::parser(route,ip,via,dev,proto,scope,src,metric);

    ui->static_ip->setText(ip);
    ui->static_via_ip->setText(via);
    ui->static_combox_interface->setCurrentText(dev);


    ui->static_check_proto->setChecked(!proto.isEmpty());
    ui->static_combox_proto->setCurrentText(proto);

    ui->static_check_scope->setChecked(!scope.isEmpty());
    ui->static_combox_scope->setCurrentText(scope);

    ui->static_check_src->setChecked(!src.isEmpty());
    ui->static_lineedit_src->setText(src);

    ui->static_check_metric->setChecked(!metric.isEmpty());
    ui->static_spinbox_metric->setValue(metric.toInt());
}

QString RouteAddDialog::route()
{
    if (ui->tabWidget->currentWidget() == ui->tab_favorite_route) {
        return favorite_route();
    }

    return static_route();
}

QString RouteAddDialog::static_route()
{
    QStringList list;
    {
        list << ui->static_ip->text();
        if (ui->static_via_ip->text().isEmpty() == false) {
            list << "via" << ui->static_via_ip->text();
        }
        list << "dev" << ui->static_combox_interface->currentText();
    }
    {
        if (ui->static_check_proto->isChecked()) {
            list << "proto" << ui->static_combox_proto->currentText();
        }
        if (ui->static_check_scope->isChecked()) {
            list << "scope" << ui->static_combox_scope->currentText();
        }
        if (ui->static_check_src->isChecked()) {
            list << "src" << ui->static_lineedit_src->text();
        }
        if (ui->static_check_metric->isChecked()) {
            list << "metric" << QString::number(ui->static_spinbox_metric->value());
        }
    }
    return list.join(" ");
}

QString RouteAddDialog::favorite_route()
{
    auto item = ui->favorite_list_routes->currentItem();
    if (item) {
        return ui->favorite_list_routes->currentItem()->text();
    }
    return QString();
}

QCompleter* RouteAddDialog::make_completer(QStringList list)
{
    auto completer = new QCompleter(this);
    auto completer_model = new QStringListModel(list);
    completer->setModel(completer_model);
    return completer;
}

void RouteAddDialog::on_static_ip_textChanged(const QString &arg1)
{
    update_static_placeholder_text();
}


void RouteAddDialog::on_static_via_ip_textChanged(const QString &arg1)
{
    update_static_placeholder_text();
}


void RouteAddDialog::on_static_combox_interface_currentTextChanged(const QString &arg1)
{
    update_static_placeholder_text();
}

void RouteAddDialog::update_static_placeholder_text()
{
    QString var = QString("%1 via %2 dev %3").arg(ui->static_ip->text()).arg(ui->static_via_ip->text()).arg(ui->static_combox_interface->currentText());

    ui->static_placeholder->setPlaceholderText(var);
}


void RouteAddDialog::on_action_favorite_add_route_triggered()
{
    auto item = ui->favorite_list_routes->currentItem();
    if (item) {

    }
}

void RouteAddDialog::on_action_favorite_delete_route_triggered()
{
    auto item = ui->favorite_list_routes->currentItem();
    if (item) {
        ui->favorite_list_routes->takeItem(ui->favorite_list_routes->currentRow());
    }
}

void RouteAddDialog::on_favorite_list_routes_customContextMenuRequested(const QPoint &pos)
{
    auto item = ui->favorite_list_routes->currentItem();

    QMenu menu;
    menu.addAction(ui->action_favorite_add_route);
    menu.addAction(ui->action_favorite_delete_route);

    ui->action_favorite_delete_route->setEnabled(item);
    menu.exec(QCursor::pos());
}

